package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.CurrentUser;
import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.domain.StrategyTheme;
import cn.wolfcode.luowowo.domain.User;
import cn.wolfcode.luowowo.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.service.IStrategyCommentService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.vo.StrategyStatisVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("strategies")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCommentService commentService;
    @Autowired
    private IStrategyStatisVORedisService voRedisService;
    //攻略明细
    @GetMapping("/detail")
    public Object detail(String id){
        Strategy strategy = strategyService.get(id);
        //阅读数+1
        voRedisService.incrementViewnum(id);
        return JsonResult.success(strategy);
    }
    //目的地下的攻略列表
    @GetMapping("/query")
    public Object query(StrategyQuery qo){
        Page<Strategy> page = strategyService.list(qo);
        return JsonResult.success(page);
    }

    //themes主题列表
    @GetMapping("/themes")
    public Object themes(){
        List<StrategyTheme> themes = strategyThemeService.allList();
        return JsonResult.success(themes);
    }

    //添加攻略评论，要求登录
    @RequireLogin
    @PostMapping("/addComment")
    public Object addComment(StrategyComment comment ,@CurrentUser User user){
        commentService.save(comment , user);
        //攻略评论数+1
        voRedisService.incrementReplynum(comment.getStrategyId());
        return JsonResult.success();
    }
    //攻略评论列表
    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){
        Page<StrategyComment> page = commentService.list(qo);
        return JsonResult.success(page);
    }
    //攻略评论点赞
    @RequireLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid , String sid,@CurrentUser User user){
        commentService.commentThumb(cid , user.getId());
        return JsonResult.success();
    }
    //获取统计数据
    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        StrategyStatisVO vo = voRedisService.get(sid);
        return JsonResult.success(vo);
    }
    //攻略收藏  true为收藏成功,false为取消收藏
    //要求登录
    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String sid,@CurrentUser User user){
        boolean b = voRedisService.favor(sid , user.getId());
        return JsonResult.success(b);
    }

    //攻略点赞，要求登录
    //顶过是true   没顶返回是false
    @RequireLogin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid,@CurrentUser User user){
        boolean b = voRedisService.strategyThumbup(sid , user.getId());
        return JsonResult.success(b);
    }
}
